package com.jokerx.ohos.fasttrpg.gamemaster;

import ohos.rpc.*;

/**
 * GameMasterServiceRemoteProxy
 *
 * @author liangxu14
 * @date 2022/5/5
 */
public class GameMasterServiceRemoteProxy implements IRemoteBroker {
    private final IRemoteObject remote;

    public GameMasterServiceRemoteProxy(IRemoteObject remote) {
        this.remote = remote;
    }

    @Override
    public IRemoteObject asObject() {
        return remote;
    }

    public void selectRole(String playerId, int roleId) {
        // 发送消息
        MessageParcel data = MessageParcel.obtain();
        data.writeInterfaceToken(GameMasterServiceConstant.DESCRIPTOR);
        data.writeString(playerId);
        data.writeInt(roleId);
        // 回复消息
        MessageParcel reply = MessageParcel.obtain();
        // 具体MessageOption的设置，可参考相关API文档
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);
        try {
            remote.sendRequest(GameMasterServiceConstant.COMMAND_SELECT_ROLE, data, reply, option);
            int errCode = reply.readInt();
            if (errCode != GameMasterServiceConstant.ERR_OK) {
                throw new RemoteException();
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        } finally{
            data.reclaim();
            reply.reclaim();
        }
    }

    public void startGame() {
        // 发送消息
        MessageParcel data = MessageParcel.obtain();
        data.writeInterfaceToken(GameMasterServiceConstant.DESCRIPTOR);
        // 回复消息
        MessageParcel reply = MessageParcel.obtain();
        // 具体MessageOption的设置，可参考相关API文档
        MessageOption option = new MessageOption(MessageOption.TF_SYNC);
        try {
            remote.sendRequest(GameMasterServiceConstant.COMMAND_START_GAME, data, reply, option);
            int errCode = reply.readInt();
            if (errCode != GameMasterServiceConstant.ERR_OK) {
                throw new RemoteException();
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        } finally{
            data.reclaim();
            reply.reclaim();
        }
    }
}
